package com.cjc.myimageload.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.cjc.myimageload.R;
import com.cjc.myimageload.lib.ImageLoader;
import com.cjc.myimageload.tools.Constants;

public class ImageAdapter extends BaseAdapter {
	private final String MY_IMAGE_LINE = "/";
	private List<Image> data;
	private ImageLoader imageLoader;
	private LayoutInflater inflater;

	public ImageAdapter(Context context, List<Image> data1, ImageLoader imageLoader) {
		inflater = LayoutInflater.from(context);
		this.data = data1;
		this.imageLoader = imageLoader;
	}

	public void updateListView(List<Image> list) {
		this.data = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_image, null);
			holder.grid_iv = (ImageView) convertView.findViewById(R.id.item_image_iv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		String Image = data.get(position).imagePic;
		String time = data.get(position).imageTime;
		System.out.println("看下图片地址：" + Image);
		if (Image != null && !"".equals(Image)) {
			// String imgURL = Constants.IMAGEURI +
			// Image.substring(Image.lastIndexOf(MY_IMAGE_LINE));
			// imageLoader.displayImage(imgURL, holder.grid_iv);
			imageLoader.displayImage(Image, time, holder.grid_iv);
		}
		return convertView;
	}

	class ViewHolder {
		ImageView grid_iv;
	}
}
